home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Misc / emu / p-interp.lha / p-interp-0.5 / svolio.c < prev    next >
C/C++ Source or Header  |  2001-06-07  |  6KB  |  304 lines

  1. /*
  2.  
  3.   P-Code interpreter (to run the apple pascal system)
  4.   Copyright (C) 2000 Mario Klebsch
  5.  
  6.   This program is free software; you can redistribute it and/or modify
  7.   it under the terms of the GNU General Public License as published by
  8.   the Free Software Foundation; either version 2 of the License, or
  9.   (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   GNU General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU General Public License
  17.   along with this program; if not, write to the Free Software
  18.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.  
  21.   $Log: svolio.c,v $
  22.   Revision 1.6  2001/06/07 21:18:15  mario
  23.   #include <getopt.h> eingefügt.
  24.  
  25.   Revision 1.5  2001/05/26 15:13:29  mario
  26.   Diverse kleine Fehler behoben, fehlende #includes, Labels ohne Statement
  27.   dahinter, ...
  28.  
  29.   Revision 1.4  2001/05/21 19:06:48  mario
  30.   Mode-Parameter bei den Disk-IO-Routinen entfernt
  31.  
  32.   Revision 1.3  2001/05/20 20:35:22  mario
  33.   svolio ruft die Disk-I/O-Routinen jetzt direkt auf.
  34.  
  35.   Revision 1.2  2001/05/20 13:12:02  mario
  36.   CVS-Idents und Logs eingefügt
  37.  
  38.  
  39. */
  40.  
  41. #ident "$Id: svolio.c,v 1.6 2001/06/07 21:18:15 mario Exp $";
  42.  
  43. #include <stdio.h>
  44. #include <unistd.h>
  45. #include <stdarg.h>
  46. #include <ctype.h>
  47. #include <getopt.h>
  48.  
  49. #include "psystem.h"
  50. #include "Memory.h"
  51. #include "Diskio.h"
  52.  
  53. word Syscom;
  54. word IoResult=0;
  55.  
  56. int List=0;
  57. int Text=0;
  58. int ExtList=0;
  59.  
  60. char *Month[16]={ "???", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
  61.           "Aug", "Sep", "Oct", "Nov", "Dec", "???", "???", "???"};
  62.  
  63. char *FileTypes[8]= {"Svol", "Bad ", "Code", "Text", 
  64.              "Info", "Data", "Graf", "Foto" };
  65.  
  66. void warning(char *Msg, ...)
  67. {
  68.   va_list ap;
  69.   char    Buffer[512];
  70.   va_start(ap, Msg);
  71.   vsnprintf(Buffer, sizeof(Buffer), Msg, ap);
  72.   va_end(ap);
  73.   fprintf(stderr,"warning: %s\n", Buffer);
  74. }
  75.  
  76. void XeqError(word err)
  77. {
  78.   fprintf(stderr,"XeqError: %d\n", err);
  79.   exit(-1);
  80. }
  81.  
  82. void IoError(word err)
  83. {
  84.   IoResult=err;
  85. }
  86.  
  87. void DoList(word Unit)
  88. {
  89.   int i;
  90.   int len;
  91.   word Entry;
  92.   word w;
  93.   word NumBlocks;
  94.   word NumFiles;
  95.   word FreeBlocks;
  96.   word MaxFree;
  97.   word LastBlock;
  98.   word Free;
  99.  
  100.   DiskRead(Unit, 0x100, 0, 2048, 2);
  101.   if (IoResult)
  102.     {
  103.       fprintf(stderr,"DoList: Directory read error\n");
  104.       exit(1);
  105.     }
  106.  
  107.   if (MemRd(WordIndexed(0x100,  8))&0xff00)
  108.     SwapBytes=!SwapBytes;
  109.  
  110.   Entry=0x100;
  111.   NumBlocks=MemRd(WordIndexed(Entry,  7));
  112.   LastBlock=MemRd(WordIndexed(Entry,  1));
  113.   NumFiles=MemRd(WordIndexed(Entry,  8));
  114.   FreeBlocks=0;
  115.   MaxFree=0;
  116.  
  117.   if (ExtList)
  118.     {
  119.       Entry=0x100;
  120.       for (len=0; len<MemRdByte(WordIndexed(Entry, 3),0); len++)
  121.     {
  122.       char ch=MemRdByte(WordIndexed(Entry, 3),1+len);
  123.       if (isupper(ch))
  124.         ch=tolower(ch);
  125.       putchar(ch);
  126.     }
  127.       putchar(':');
  128.       putchar('\n');
  129.     }
  130.   
  131.   for (i=0;i<NumFiles;i++)
  132.     {
  133.       Entry=WordIndexed(0x100, 13+13*i);
  134.  
  135.       if (ExtList)
  136.     if (MemRd(WordIndexed(Entry,  0))>LastBlock)
  137.       {
  138.         Free=MemRd(WordIndexed(Entry,  0))-LastBlock;
  139.         printf("< unused >     %5d           %5d\n", Free, LastBlock);
  140.         FreeBlocks += Free;
  141.         if (Free>MaxFree)
  142.           MaxFree=Free;
  143.       }
  144.       LastBlock=MemRd(WordIndexed(Entry,  1));
  145.  
  146.       for (len=0; len<MemRdByte(WordIndexed(Entry, 3),0); len++)
  147.     {
  148.       char ch=MemRdByte(WordIndexed(Entry, 3),1+len);
  149.       if (isupper(ch))
  150.         ch=tolower(ch);
  151.       putchar(ch);
  152.     }
  153.  
  154.       if (ExtList)
  155.     {
  156.       for (; len<15; len++)
  157.         putchar(' ');
  158.       
  159.       w=MemRd(WordIndexed(Entry, 12));
  160.       printf("%5d %2d-%3s-%02d %5d %5d %4sfile",
  161.          MemRd(WordIndexed(Entry, 1)) - MemRd(WordIndexed(Entry, 0)),
  162.          (w>>4)&0x1f, Month[w&0x0f], w>>9,
  163.          MemRd(WordIndexed(Entry,  0)),
  164.          MemRd(WordIndexed(Entry, 11)),
  165.          FileTypes[MemRd(WordIndexed(Entry, 2))&0x07]);
  166.     }
  167.       putchar('\n');
  168.     }
  169.  
  170.   if (ExtList)
  171.     {
  172.       if (NumBlocks>LastBlock)
  173.     {
  174.       Free=NumBlocks-LastBlock;
  175.       printf("< unused >     %5d           %5d\n", Free, LastBlock);
  176.       FreeBlocks += Free;
  177.       if (Free>MaxFree)
  178.         MaxFree=Free;
  179.     }
  180.       printf("%d files, %d blocks used, %d unused, %d in largest\n",
  181.          NumFiles, NumBlocks-FreeBlocks, FreeBlocks, MaxFree);
  182.     }
  183. }
  184.  
  185. word LookupFile(word Unit, const char *Name, word *Offset,
  186.         word *Len, word *Last)
  187. {
  188.   int i;
  189.   DiskRead(Unit, 0x100, 0, 2048, 2);
  190.   if (IoResult)
  191.     return(0);
  192.  
  193.   if (MemRd(WordIndexed(0x100,  8))&0xff00)
  194.     SwapBytes=!SwapBytes;
  195.  
  196.   for (i=0;i<MemRd(WordIndexed(0x100, 8));i++)
  197.     {
  198.       word Entry=WordIndexed(0x100, 13+13*i);
  199.       int len;
  200.       for (len=0; len<MemRdByte(WordIndexed(Entry, 3),0); len++)
  201.     if (toupper(MemRdByte(WordIndexed(Entry, 3),1+len)) !=
  202.         toupper(Name[len]))
  203.       goto next;
  204.       if (Name[len])
  205.     continue;
  206.     
  207.       *Offset=MemRd(WordIndexed(Entry,0));
  208.       *Len=MemRd(WordIndexed(Entry,1));
  209.       *Last=MemRd(WordIndexed(Entry,11));
  210.  
  211.       return(1);
  212.     next:
  213.       ;
  214.     }
  215.   return(0);
  216. }
  217.  
  218. int main(int argc, char *argv[])
  219. {
  220.   int i;
  221.  
  222.   Syscom=WordIndexed(0xfffe, -SYSCOM_SIZE);
  223.  
  224.   while ((i=getopt(argc, argv, "elt"))!=EOF)
  225.     switch(i)
  226.       {
  227.       case 'e':
  228.     ExtList=1;
  229.     List=1;
  230.     break;
  231.       case 'l':
  232.     List=1;
  233.     break;
  234.       case 't':
  235.     Text=1;
  236.     break;
  237.       }
  238.  
  239.   if (optind<argc)
  240.     DiskMount(4, argv[optind++], ReadOnly);
  241.   else
  242.     {
  243.       fprintf(stderr,"%s: Needs filename argument\n", argv[0]);
  244.       exit(1);
  245.     }
  246.  
  247.   if (List)
  248.     {
  249.       DoList(4);
  250.       exit(0);
  251.     }
  252.   if (optind<argc)
  253.     {
  254.       word Offset, End, Last;
  255.       char *name=argv[optind++];
  256.       if (!LookupFile(4, name, &Offset, &End, &Last))
  257.     {
  258.       fprintf(stderr, "%s: File not found\n", name);
  259.       exit(1);
  260.     }
  261.  
  262.       if (Text)
  263.     Offset +=2;
  264.  
  265.       while (Offset<End)
  266.     {
  267.       int Size=(End-Offset-1)*512+Last;
  268.       if (Size>1024)
  269.         Size=1024;
  270.       DiskRead(4, 0x100, 0, Size, Offset);
  271.       for (i=0; i<Size; i++)
  272.         {
  273.           char ch=MemRdByte(0x100,i);
  274.           if (Text)
  275.         {
  276.           if (ch==0x10)
  277.             {
  278.               i++;
  279.               for (ch=MemRdByte(0x100,i); ch>0x20; ch--)
  280.             write(1," ", 1);
  281.             }
  282.           else if (ch==0)
  283.             break;
  284.           else
  285.             {
  286.               if (ch==0x0d)
  287.             ch='\n';
  288.               write(1, &ch, 1);
  289.             }
  290.         }
  291.           else
  292.         write(1, &ch, 1);
  293.         }
  294.       Offset+=2;
  295.     }
  296.     }
  297.   else
  298.     {
  299.       fprintf(stderr,"%s: Needs filename argument\n", argv[0]);
  300.       exit(1);
  301.     }
  302.   exit(0);
  303. }
  304.